#ifndef SAMPLINGMANAGER_H
#define SAMPLINGMANAGER_H

#include <QObject>
#include <QTimer>
#include <QQueue>
#include <QDateTime>
#include "devicecontroller.h"
#include "auditlogger.h"

// 采样任务结构体
typedef struct {
    int task_id;            // 任务ID
    QString task_name;      // 任务名称
    SamplingMode mode;      // 采样模式
    int parameter;          // 参数(时间或体积)
    QDateTime scheduled_time; // 计划执行时间
    QString operator_id;    // 操作员ID
    bool is_recurring;      // 是否重复执行
    int recurrence_interval; // 重复间隔(分钟)
    bool is_active;         // 是否激活
} SamplingTask;

// 采样计划状态
typedef enum {
    SCHEDULE_IDLE = 0,      // 空闲
    SCHEDULE_WAITING,       // 等待执行
    SCHEDULE_EXECUTING,     // 正在执行
    SCHEDULE_PAUSED,        // 已暂停
    SCHEDULE_ERROR          // 错误状态
} ScheduleStatus;

class SamplingManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString scheduleStatus READ getScheduleStatusString NOTIFY scheduleStatusChanged)
    Q_PROPERTY(int pendingTasks READ getPendingTasksCount NOTIFY pendingTasksChanged)
    Q_PROPERTY(int completedTasks READ getCompletedTasksCount NOTIFY completedTasksChanged)

public:
    explicit SamplingManager(QObject *parent = nullptr);
    ~SamplingManager();

    // 初始化
    bool initialize(DeviceController* deviceController, AuditLogger* auditLogger);

    // 任务管理方法
    Q_INVOKABLE int createTimedSamplingTask(const QString& taskName, int samplingTime, 
                                           const QDateTime& scheduledTime, bool isRecurring = false);
    Q_INVOKABLE int createVolumeSamplingTask(const QString& taskName, int samplingVolume, 
                                            const QDateTime& scheduledTime, bool isRecurring = false);
    Q_INVOKABLE bool deleteTask(int taskId);
    Q_INVOKABLE bool pauseTask(int taskId);
    Q_INVOKABLE bool resumeTask(int taskId);
    Q_INVOKABLE bool executeTaskNow(int taskId);
    
    // 查询方法
    Q_INVOKABLE QList<QVariant> getAllTasks() const;
    Q_INVOKABLE QList<QVariant> getActiveTasks() const;
    Q_INVOKABLE QVariant getTask(int taskId) const;
    Q_INVOKABLE int getPendingTasksCount() const;
    Q_INVOKABLE int getCompletedTasksCount() const;
    
    // 状态查询
    Q_INVOKABLE ScheduleStatus getScheduleStatus() const;
    Q_INVOKABLE QString getScheduleStatusString() const;
    Q_INVOKABLE QDateTime getNextScheduledTime() const;
    
    // 批量操作
    Q_INVOKABLE bool pauseAllTasks();
    Q_INVOKABLE bool resumeAllTasks();
    Q_INVOKABLE bool clearCompletedTasks();
    
    // 导入导出
    Q_INVOKABLE bool exportTasks(const QString& filePath) const;
    Q_INVOKABLE bool importTasks(const QString& filePath);

signals:
    void scheduleStatusChanged(ScheduleStatus status);
    void pendingTasksChanged(int count);
    void completedTasksChanged(int count);
    void taskCreated(int taskId, const QString& taskName);
    void taskStarted(int taskId);
    void taskCompleted(int taskId, bool success);
    void taskError(int taskId, const QString& error);
    void nextTaskScheduled(const QDateTime& scheduledTime);

private slots:
    void checkScheduledTasks();
    void onDeviceSamplingCompleted(const SamplingRecord& record);
    void onDeviceError(const QString& error);

private:
    // 私有成员变量
    DeviceController* m_deviceController;
    AuditLogger* m_auditLogger;
    QTimer* m_scheduleTimer;
    
    QList<SamplingTask> m_tasks;
    QQueue<int> m_taskQueue;
    ScheduleStatus m_scheduleStatus;
    int m_nextTaskId;
    int m_currentTaskId;
    int m_completedTasksCount;
    
    // 私有方法 (使用C风格语法)
    bool executeTask(int taskId);
    void updateScheduleStatus();
    void logTaskOperation(int taskId, const char* operation, const char* details);
    SamplingTask* findTask(int taskId);
    void scheduleNextTask();
    bool validateTaskParameters(const SamplingTask& task);
    void removeExpiredTasks();
    
    // 任务持久化
    bool saveTasksToDatabase();
    bool loadTasksFromDatabase();
    void createTasksTable();
};

// C风格的辅助函数
extern "C" {
    const char* scheduleStatusToString(ScheduleStatus status);
    bool isValidTaskId(int taskId);
    int calculateNextRecurrence(int intervalMinutes);
    bool isTaskExpired(const SamplingTask* task);
}

#endif // SAMPLINGMANAGER_H